/*
 * comlan
 */

package com.promfy.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileUtil {
	/**
	 * copy files, contains file and directory.
	 *  
	 * @param url1
	 * @param url2
	 * @throws IOException
	 */
	public static void copyFiles(String url1, String url2) throws IOException {
		File f = new File(url1);
		if(!f.exists()) return;
		(new File(url2)).mkdirs();

		File[] file = (new File(url1)).listFiles();
		if(file == null) return;
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {

				copyFile(file[i], new File(url2 + file[i].getName()));
			}
			if (file[i].isDirectory()) {

				String sourceDir = url1 + File.separator + file[i].getName();
				String targetDir = url2 + File.separator + file[i].getName();
				copyDirectiory(sourceDir, targetDir);
			}
		}
	}

	/**
	 * copy files, contains file and directory.
	 * 
	 * @param url1
	 * @param url2
	 * @param string 
	 * @throws IOException
	 */
	public static void copyFileToFile(String url1, String url2Path, String url2Name) throws IOException {
		

		File file = new File(url1);
		if(file == null) return;
		File file2 = new File(url2Path);
		if(!file2.exists()) file2.mkdirs();
		copyFile(file, new File(url2Path + url2Name));
	}
	/**
	 * copy file.
	 * 
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 */
	public static void copyFile(File sourceFile, File targetFile) throws IOException {

		FileInputStream input = null;
		BufferedInputStream inBuff = null;

		FileOutputStream output = null;
		BufferedOutputStream outBuff = null;
		try {
			input = new FileInputStream(sourceFile);
			inBuff = new BufferedInputStream(input);

			output = new FileOutputStream(targetFile);
			outBuff = new BufferedOutputStream(output); 

			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			outBuff.flush();
			output.flush();
		} catch (Exception e) { 
			e.printStackTrace();
		} finally {
			try {
				if (outBuff != null) {
					outBuff.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				outBuff = null;
			}
			try {
				if (output != null) {
					output.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				output = null;
			}
			try {
				if (inBuff != null) {
					inBuff.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				inBuff = null;
			}
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				input = null;
			}
		}
	}

	/**
	 * copy directory.
	 * 
	 * @param sourceDir
	 * @param targetDir
	 * @throws IOException
	 */
	public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {

		(new File(targetDir)).mkdirs();

		File[] file = (new File(sourceDir)).listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {

				File sourceFile = file[i];

				File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
				copyFile(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {

				String dir1 = sourceDir + "/" + file[i].getName();

				String dir2 = targetDir + "/" + file[i].getName();
				copyDirectiory(dir1, dir2);
			}
		}
	}

	/**
	 * delete files, contains file and directory.
	 * 
	 * @param path
	 */
	public static void deleteFiles(String path) throws Exception{
		File f = new File(path);
		if (!f.exists()) {
			return;
		}
		if (f.delete()) {
			// System.out.println("delete directory : " + f.getAbsolutePath());
		} else {
			File[] fs = f.listFiles();
			for (int i = 0; i < fs.length; i++) {
				if (fs[i].isDirectory()) {
					if (!fs[i].delete()) {
						deleteFiles(fs[i].getAbsolutePath());
					} else {
						// System.out.println("delete directory : " +
						// fs[i].getAbsolutePath());
					}
				} else {
					fs[i].delete();
					// System.out.println("delete file : " +
					// fs[i].getAbsolutePath());
				}
			}
			f.delete();
			// System.out.println("delete directory : " + f.getAbsolutePath());
		}
	}
	
	public static String getStringFromFiles(String keyPath) throws Exception {
		FileInputStream inputStream = null;
		BufferedReader reader = null;
		StringBuilder stringBuilder = new StringBuilder();
		try {
			 inputStream = new FileInputStream(keyPath);
			 reader = new BufferedReader(new InputStreamReader(inputStream));
			for (String line = reader.readLine(); line != null; line = reader.readLine()) {
				stringBuilder.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(inputStream != null){				
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return stringBuilder.toString();
	}
	
	public static void writeStringToFile(String str, String fileName){
		BufferedWriter  buff = null;
		try {
			buff = new BufferedWriter(new FileWriter(fileName));  
			for (int i = 0; i < str.length(); i++) {  
			      buff.write(str.charAt(i));  
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(buff != null){
				try {
					buff.close();
				} catch (IOException e) {
					e.printStackTrace();
					buff = null;
				}
			}
		}
	}
}
